package gold.contest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC5715 {
    public long startExecuteTime = System.currentTimeMillis();


    //    如果 i % 2 == 0 ，那么 arr[i] = perm[i / 2]
//    如果 i % 2 == 1 ，那么 arr[i] = perm[n / 2 + (i - 1) / 2]
    class Solution {
        public int reinitializePermutation(int n) {
            int step = 0;
            int[] arrCur = new int[n];
            for (int i = 0; i < n; i++) {
                arrCur[i] = i;
            }
            String standard = Arrays.toString(arrCur);

            while (true) {
                int[] arrNext = doChange(arrCur);
                ++step;
                if (Arrays.toString(arrNext).equals(standard)) break;
                arrCur = arrNext;
            }

            return step;
        }

        public int[] doChange(int[] arrCur) {
            int[] arrNext = new int[arrCur.length];
            for (int i = 0; i < arrCur.length; i++) {
                if (i % 2 == 0) arrNext[i] = arrCur[i / 2];
                else arrNext[i] = arrCur[arrCur.length / 2 + (i - 1) / 2];
            }

            return arrNext;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.reinitializePermutation(6));
    }

    public static void main(String[] args) throws Exception {
        LC5715 an = new LC5715();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
